// pages/relName/relName.js
const app = getApp();
const ajax = require("../../utils/ajax.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radioIndex: 0,
    sexIndex: 0,
    doctor: {
      experts: [],
      real_name: "",
      idcard: "",
      idcard_photo: "", //身份证照片
      license_number: "", //证件号码
      license: "", //证件照片
      cellphone: "",
      hospital: "",
      address: "",
      job: "",
      sex: 0,
      description: ""
    },
    eValue: "",
    type: "",
    idcardImg: "",
    licenseImg: ""
  },
  clearClick:function(e){
    //清除
    let type = e.currentTarget.dataset.type
    let doctorData = this.data.doctor;
    if (type == "idcard") {
      doctorData.idcard_photo=""
      this.setData({
        doctor:doctorData,
        idcardImg:""
      })
    } else if (type == "license") {
      doctorData.license = ""
      this.setData({
        doctor: doctorData,
        licenseImg: ""
      })
    }
  },
  uploadImgClick: function(e) {
    let type=e.currentTarget.dataset.type
    //上传证件照
    let that = this;
    let doctorData = this.data.doctor;
    //上传图片
    if(type=="idcard"){
      let imgUrl = this.data.idcardImg;
    } else if (type =="license"){
      let imgUrl = this.data.license;
    }
    
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        let token = wx.getStorageSync("token")
        wx.showLoading({
          title: '拼命上传中..',
          icon: "none"
        })
        let pathArr = res.tempFilePaths
        for (var i = 0; i < pathArr.length; i++) {
          wx.uploadFile({
            url: app.url.upload, //仅为示例，非真实的接口地址
            filePath: res.tempFilePaths[i],
            name: 'file',
            formData: {
              token: token
            },
            success: function(img) {
              let data = JSON.parse(img.data)
              if (data.code !== 200) {
                return wx.showToast({
                  title: data.msg,
                  icon: "none"
                })
              }
              if (type == "idcard") {
                doctorData.idcard_photo = JSON.parse(img.data).data.path
                that.setData({
                  idcardImg: JSON.parse(img.data).data.url
                })
              } else if (type == "license") {
                doctorData.license = JSON.parse(img.data).data.path
                that.setData({
                  licenseImg: JSON.parse(img.data).data.url
                })
              }
              that.setData({
                doctor: doctorData
              })
              wx.hideLoading()
            }
          })
        }
      }
    })
  },
  //提交
  submitClick: function() {
    //验证信息
    let doctor = this.data.doctor
    if (!app.reg.idCard.test(doctor.idcard)) {
      return wx.showToast({
        title: '请输入正确的身份证信息',
        icon: 'none'
      })
    }
    if (doctor.idcard_photo == "") {
      return wx.showToast({
        title: '请上传身份证人像面图片',
        icon: 'none'
      })
    }
    if (doctor.license_number == "") {
      return wx.showToast({
        title: '请输入执证代码',
        icon: 'none'
      })
    }
    if (doctor.license == "") {
      return wx.showToast({
        title: '请上传执证许可证或营业执照',
        icon: 'none'
      })
    }
    if (!app.reg.phone.test(doctor.cellphone)) {
      return wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
    }
    
    ajax.request({
      url: app.url.doctorAdd,
      data: doctor
    }).then(res => {
      if (res.code != 200) {
        return wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
      doctor.doctor_id = res.data
      let str = JSON.stringify(doctor)
      wx.setStorageSync("userInfo", str)
      if (this.data.type) {
        wx.showToast({
          title: '修改成功',
          icon: "none",
          success: function() {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      } else {
        wx.removeStorageSync("token")
        wx.removeStorageSync("userInfo")
        wx.showToast({
          title: '您的信息已提交，妙手春花会在2个工作日内给您回复，请耐心等待，谢谢！',
          icon: "none",
          duration: 3000,
          success: function() {
            setTimeout(function() {
              wx.reLaunch({
                url: '/pages/login/login',
              })
            }, 2000);
          }
        })
      }

    });
  },
  expertValue(e) {
    this.setData({
      eValue: e.detail.value
    })
  },
  addExpert() {
    if (this.data.eValue == "") {
      return wx.showToast({
        title: '请输入擅长信息',
        icon: 'none'
      });
    }
    let experts = this.data.doctor.experts
    if (experts.length >= 5) {
      return wx.showToast({
        title: '最多可输入5个擅长领域',
        icon: 'none'
      });
    }

    console.log(this.data.eValue);
    console.log(experts)
    if (experts.length == 0) {
      experts.push(this.data.eValue)
    } else {

      if (this.inArray(this.data.eValue, experts)) {
        return wx.showToast({
          title: '已输入的擅长信息',
          icon: 'none'
        });
      }
      experts.push(this.data.eValue)
    }
    console.log(experts)
    let doctor = this.data.doctor;
    doctor.experts = experts;
    this.setData({
      doctor: doctor,
      eValue: ""
    })
  },
  deleteExpert(e) {
    let v = e.currentTarget.dataset.value;
    let experts = this.data.doctor.experts;
    let that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除该擅长信息',
      success: function(res) {
        if (res.confirm) {
          let es = [];
          for (var i in experts) {
            if (experts[i] == v) {
              continue
            }
            es.push(experts[i])
          }
          let doctor = that.data.doctor;
          doctor.experts = es;
          that.setData({
            doctor: doctor
          })
        } else if (res.cancel) {

        }
      }
    })


  },
  inArray(v, arr) {
    for (var i in arr) {
      if (arr[i] == v) {
        return true
      }
    }
    return false;
  },
  //事件处理函数  
  sexClick: function(event) {
    let doctor = this.data.doctor;
    doctor.sex = event.target.dataset.sex;
    this.setData({
      doctor: doctor
    })
  },
  changeDoctor(e) {
    let key = e.currentTarget.dataset.key;
    let doctor = this.data.doctor;
    doctor[key] = e.detail.value
    this.setData({
      doctor: doctor
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    if (options.type) {
      this.setData({
        type: options.type
      })
      wx.setNavigationBarTitle({
        title: '修改信息'
      });
    } else {
      wx.setNavigationBarTitle({
        title: '实名认证'
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let user = app.getLoginUserInfo()
    if (user.doctor_id > 0) {
      if (user.doctor_status == 0) {
        return wx.showToast({
          title: '信息待审核',
          icon: 'none',
          success: function() {
            wx.removeStorageSync("token")
            wx.removeStorageSync("userInfo")
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        })
      } else if (user.doctor_status == 3) {
        return wx.showToast({
          title: '该用户已禁用',
          icon: 'none',
          success: function() {
            wx.removeStorageSync("token")
            wx.removeStorageSync("userInfo")
          }
        })
      }

    }
    if (!user.cellphone) {
      user.cellphone = user.phone;
    }
    this.setData({
      doctor: user
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})